﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Helpers
{
    /// <summary>
    /// 配置帮助类，依赖于DiHelper
    /// </summary>
    public class ConfigHelper
    {
        private readonly IConfiguration _configuration;

        public ConfigHelper(IConfiguration configuration)
        {
            this._configuration = configuration;
        }
        ///// <summary>
        ///// 配置实例
        ///// </summary>
        //public static IConfiguration Configuration => DiHelper.ServiceProvider.GetRequiredService<IConfiguration>();

        ///// <summary>
        ///// 配置实例化
        ///// </summary>
        //public static T GetAppSettings<T>(string key) where T : class, new()
        //{
        //    return Helpers.DiHelper.ServiceCollection
        //       .Configure<T>(Configuration.GetSection(key))//注入配置数据
        //       .BuildServiceProvider()
        //       .GetService<IOptions<T>>()
        //       .Value;
        //}

        /// <summary>
        /// 配置实例化
        /// </summary>
        public T GetAppSettings<T>(string key) where T : class, new()
        {
            return string.IsNullOrEmpty(key) ? _configuration.Get<T>() : _configuration.GetSection(key).Get<T>();
        }


        /// <summary>
        /// 创建配置
        /// 读取顺序：JsonFile,EnvironmentVariables,UserSecrets,MemoryCollection,CommandLine
        /// </summary>
        public static IConfiguration CreateConfiguration(
            IConfigurationBuilder builder = null,
            string[] jsonFileNames = null,
            string secretsId = null,
            IEnumerable<KeyValuePair<string, string>> memoryData = null,
            string[] commandLineArgs = null,
            string ASPNETCORE_ENVIRONMENT=null)
        {
            builder = builder ?? new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory);
            //AddJsonFile
            ASPNETCORE_ENVIRONMENT =
                Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Process)
                ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.User)
                ?? Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT", EnvironmentVariableTarget.Machine);
            for (int i = 0; i < jsonFileNames?.Length; i++)
            {
                builder.AddJsonFile($"{jsonFileNames[i]}.json", false, true);
                if (ASPNETCORE_ENVIRONMENT != null)
                    builder.AddJsonFile($"{jsonFileNames[i]}.{ASPNETCORE_ENVIRONMENT}.json", true, true);
            }

            //AddEnvironmentVariables
            builder.AddEnvironmentVariables();

            //AddUserSecrets
            if (!string.IsNullOrEmpty(secretsId))
                builder.AddUserSecrets(secretsId);

            //AddInMemoryCollection
            if (memoryData != null)
                builder.AddInMemoryCollection(memoryData);

            //AddCommandLine
            if (commandLineArgs != null)
                builder.AddCommandLine(commandLineArgs);

            var config = builder.Build();
            return config;
        }

        
    }
}


